package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjGoodsParam;
import com.zb.entity.ZjProductParam;
import com.zb.entity.ZjProductParamFieldVal;
import com.zb.extend.ProductParamFieldValExtend;
import com.zb.mapper.ZjProductParamMapper;
import com.zb.service.ZjGoodsParamService;
import com.zb.service.ZjProductParamFieldValOptionService;
import com.zb.service.ZjProductParamFieldValService;
import com.zb.service.ZjProductParamService;
import com.zb.vo.GoodsCustomParamVo;
import com.zb.vo.GoodsDetailParamVo;
import com.zb.vo.GoodsFieldValVo;
import com.zb.vo.GoodsParamValVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 产品参数关联表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-15
 */
@Service
public class ZjProductParamServiceImpl extends ServiceImpl<ZjProductParamMapper, ZjProductParam> implements ZjProductParamService {
    @Resource
    private ZjProductParamFieldValService paramFieldValService;
    @Resource
    private ZjGoodsParamService goodsParamService;


    // 新增或修改商品参数 & 商品参数字段
    @Override
    @Transactional
    public void save(Long custAccId, Long productId, GoodsParamValVo paramValVo) {
        if (null == paramValVo || null == paramValVo.getParamId() || 0 == paramValVo.getParamId()) {
            return;
        }
        ZjProductParam param = new ZjProductParam();
        param.setId(paramValVo.getId());
        param.setParamId(paramValVo.getParamId());
        if (null != param.getId()) {
            this.updateById(param);
        } else {
            QueryWrapper<ZjProductParam> wrapper = new QueryWrapper<>();
            wrapper.select("id");
            wrapper.eq("product_id", productId);
            ZjProductParam param1 = this.getOne(wrapper);
            if (null != param1) {
                param.setId(param1.getId());
                this.updateById(param);
            } else {
                param.setId(IdWorker.getId());
                param.setCustAccId(custAccId);
                param.setProductId(productId);
                this.save(param);
            }
        }
        paramFieldValService.save(custAccId, param.getId(), paramValVo.getFieldValVos());
    }

    // 根据产品ID获取参数名称
    @Override
    public GoodsDetailParamVo getByProductId(Long productId) {
        QueryWrapper<ZjProductParam> wrapper = new QueryWrapper<>();
        wrapper.select("id", "param_id");
        wrapper.eq("product_id", productId);
        ZjProductParam param = this.getOne(wrapper);
        if (null == param) {
            return null;
        }
        ZjGoodsParam goodsParam = goodsParamService.getById(param.getParamId());
        if (null == goodsParam) {
            return null;
        }
        List<ProductParamFieldValExtend> paramFieldValList = paramFieldValService.getByProductParamId(param.getId());
        List<GoodsCustomParamVo> paramFields = paramFieldValList.stream().map(vo -> {
            GoodsCustomParamVo paramField = new GoodsCustomParamVo();
            BeanUtil.copyProperties(vo, paramField);
            return paramField;
        }).toList();

        GoodsDetailParamVo paramVo = new GoodsDetailParamVo();
        paramVo.setName(goodsParam.getName());
        paramVo.setParamFields(paramFields);
        return paramVo;
    }

    @Override
    public void deleteBatchByProductId(List<Long> productIds) {
        if (null == productIds || productIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjProductParam> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.in("product_id", productIds);
        List<ZjProductParam> params = this.list(wrapper);
        List<Long> ids = params.stream().map(ZjProductParam::getId).toList();
        paramFieldValService.deleteByProductParamIds(ids);
        this.removeByIds(ids);
    }

    // 根据产品ID获取产品参数 及参数字段
    @Override
    public GoodsParamValVo getParamByProductId(Long productId) {
        QueryWrapper<ZjProductParam> wrapper = new QueryWrapper<>();
        wrapper.select("id", "param_id");
        wrapper.eq("product_id", productId);
        ZjProductParam param = this.getOne(wrapper);
        if (null == param) {
            return null;
        }
        GoodsParamValVo vo = new GoodsParamValVo();
        vo.setId(param.getId());
        vo.setParamId(param.getParamId());
        vo.setFieldValVos(paramFieldValService.getByProductParamId(param.getId()).stream().map(field -> {
            GoodsFieldValVo fieldValVo = new GoodsFieldValVo();
            BeanUtils.copyProperties(field, fieldValVo);
            return fieldValVo;
        }).toList());
        return vo;
    }
}
